package com.ocean.client.extension.security.filter;

import com.ocean.client.extension.security.core.UmsMemberDetails;
import com.ocean.core.exception.verify.BadException;
import com.ocean.core.util.constants.ConstantsAttribute;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * 全局过滤器
 *
 * @author zhang
 * @since 2023/11/24
 */
@Slf4j
@Component
public class WebFilterExtend {

    /**
     * 获取用户信息
     *
     * @return SysUserDetails
     */
    public static UmsMemberDetails current() {
        try {
            return (UmsMemberDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            log.error("你似乎放行了必须登录的接口...");
            throw new BadException(ConstantsAttribute.NO_AUTHORIZATION);
        }
    }

    public static Long getMemberId() {
        return current().getMemberId();
    }
}
